﻿/****************************** Module Header ******************************\
 * Module Name:  WebPaggerGeneric.cs 已废除,推荐使用WebPaginnation
 * Module Version:  1.0.0 (2012-8-23)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to collections object's pagging. It supplies the pagger
 * object's attributes and pagging method.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace UCF.Core.Web
{
    public class WebPaggerGeneric<T> : PaggerGeneric<T>, IWebPagger
    {
        /// <summary>
        /// 获取带页码的链接
        /// </summary>
        /// <param name="pageid"></param>
        /// <param name="varname"></param>
        /// <returns></returns>
        public string GetPageLink(int pageid, string varname = "page")
        {
            System.Web.HttpRequest q = System.Web.HttpContext.Current.Request;
            if (q.QueryString.ToString().Length == 0)
            {
                return "{0}?{1}={2}".Fmt(q.Url.AbsolutePath, varname, pageid);
            }
            return "{0}?{1}".Fmt(q.Url.AbsolutePath, q.Url.Query.TrimStart('?').ReplaceKeyValue(varname, pageid.ToString()));
        }

        /// <summary>
        /// 获取指定长度的页码列表
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public Dictionary<int, string> GetPageIndexList(int length = 5, string varname = "page")
        {
            Dictionary<int, string> list = new Dictionary<int, string>();

            // 页数太小直接返回页数
            if (this.PageCount <= length)
            {
                for (int i = 1; i <= this.PageCount; i++)
                {
                    list.Add(i, this.GetPageLink(i, varname));
                }
                return list;
            }
            int n = length / 2;
            int n1 = this.PageIndex - n;
            int n2 = this.PageIndex + n;
            if (n1 < 1)
            {
                n2 += 1 - n1;
                n1 = 1;
            }
            if (n2 > this.PageCount)
            {
                n2 = this.PageCount;
            }
            while (n1 <= n2)
            {
                list.Add(n1, this.GetPageLink(n1, varname));
                n1++;
            }
            return list;
        }

        /// <summary>
        /// 获取分页条字符串
        /// </summary>
        /// <param name="varname"></param>
        /// <returns></returns>
        public string GetPaggerBarText(string varname = "page")
        {
            System.Web.HttpResponse w = System.Web.HttpContext.Current.Response;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<a href='{0}'>第一页</a> <a href='{1}'>上一页</a> <a href='{2}'>下一页</a> <a href='{3}'>最末页</a> {4}/{5} 页次 总{6}条 每页{7}条", this.GetPageLink(1, varname), this.GetPageLink(this.PrevPage, varname), this.GetPageLink(this.NextPage, varname), this.GetPageLink(this.LastPage, varname), this.PageIndex, this.PageCount, this.RecordCount, this.PageSize);
            return sb.ToString();
        }

        /// <summary>
        /// 获取分页页码列表
        /// </summary>
        /// <param name="length"></param>
        /// <param name="varname"></param>
        /// <returns></returns>
        public string GetPaggerIndexListText(int length = 5, string varname = "page")
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<int, string> list = this.GetPageIndexList(length, varname);
            foreach (int i in list.Keys)
            {
                sb.AppendFormat("<a href='{0}'>{1}</a>", list[i], i);
            }
            return sb.ToString();
        }
    }
}
